﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class ManageOrders : Form
    {
        public ManageOrders()
        {
            InitializeComponent();
        }
        string connectionString = Class1.connectionString;

        private void ManageOrders_Load(object sender, EventArgs e)
        {
            LoadOrders(); // 窗体加载时显示订单列表
        }
        private void dataGridViewOrders_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            LoadOrders();// 窗体加载时显示订单列表
        }

        private void LoadOrders()
        {
            string query = "SELECT * FROM OrderMain"; // 确保查询语句正确
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
                DataTable dt = new DataTable();
                adapter.Fill(dt); // 将查询结果填充到 DataTable
                dataGridViewOrders.DataSource = dt; // 绑定 DataTable 到 DataGridView
                
            }
        }
        private void btnAddOrder_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtOrderID.Text) ||
                string.IsNullOrWhiteSpace(txtStudentID.Text) ||
                string.IsNullOrWhiteSpace(txtPaymentMethod.Text))
            {
                MessageBox.Show("请输入所有必填字段！");
                return;
            }
            int orderID, studentID;
            if (!int.TryParse(txtOrderID.Text, out orderID) || !int.TryParse(txtStudentID.Text, out studentID))
            {
                MessageBox.Show("OrderID 和 StudentID 必须是有效的数字！");
                return;
            }
            string query = "INSERT INTO OrderMain (OrderID, StudentID, OrderDate, PaymentMethod) " +
                           "VALUES (@OrderID, @StudentID, @OrderDate, @PaymentMethod)";
            using (SqlConnection conn = new SqlConnection(Class1.connectionString))
            {
                try
                {
                    conn.Open();

                    // 检查 StudentID 是否存在
                    SqlCommand checkCmd = new SqlCommand("SELECT COUNT(*) FROM Student WHERE StudentID = @StudentID", conn);
                    checkCmd.Parameters.AddWithValue("@StudentID", studentID);
                    int count = (int)checkCmd.ExecuteScalar();
                    if (count == 0)
                    {
                        MessageBox.Show("StudentID 不存在，请输入有效的 StudentID！");
                        return;
                    }

                    // 插入订单
                    SqlCommand cmd = new SqlCommand(query, conn);
                    cmd.Parameters.AddWithValue("@OrderID", orderID);
                    cmd.Parameters.AddWithValue("@StudentID", studentID);
                    cmd.Parameters.AddWithValue("@OrderDate", DateTime.Now);
                    cmd.Parameters.AddWithValue("@PaymentMethod", txtPaymentMethod.Text);

                    int rowsAffected = cmd.ExecuteNonQuery();
                    if (rowsAffected > 0)
                    {
                        MessageBox.Show("订单添加成功！");
                        LoadOrders(); // 刷新订单列表
                    }
                    else
                    {
                        MessageBox.Show("订单添加失败！");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("发生错误：" + ex.Message);
                }
                LoadOrders();
            }
        }


        private void btnDeleteOrder_Click(object sender, EventArgs e)
        {
            string query = "DELETE FROM OrderMain WHERE OrderID = @OrderID";
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@OrderID", txtOrderID.Text);
                conn.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("订单删除成功！");
                LoadOrders();
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}
